
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:thingskit/components/selectBox.dart';

import '../../../utils/const.dart';

class RfCollapseController extends GetxController {
  final isForward = false.obs; //是否转发
  final ttlCount = TextEditingController(text: '0'); // 转发次数
  final isNetwork = false.obs; //是否联网
  final power = 0.0.obs; // 发射功率

  void setDefault() {
    isForward.value = false;
    ttlCount.text = '0';
    isNetwork.value = false;
    power.value = 0.0;
  }

  void setModeFromJson(Map<String, dynamic> json) {
    isForward.value = json['forwarding'] == 1;
    ttlCount.text = json['ttl_count'].toString();
    isNetwork.value = json['network'] == 1;
    power.value = json['tx_power'].toDouble();
  }
}

class RFCollapse extends StatelessWidget {
  RFCollapse({super.key});

  final RfCollapseController controller = Get.put(RfCollapseController());

  final List<String> level = ['低', '中', '高'];

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Obx(
                () => Text(
                  '发射功率：${level[controller.power.value.toInt()]}',
                  style: TextStyle(fontSize: 18),
                ),
              ),
              SizedBox(width: 10),
              Obx(
                () => Slider(
                  min: 0,
                  max: 2.9,
                  activeColor: Colors.blue,
                  value: controller.power.value,
                  onChanged: (value) {
                    controller.power.value = value;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              SizedBox(
                width: 80,
                child: Text('是否转发', style: TextStyle(fontSize: 18)),
              ),
              Obx(
                () => Switch(
                  activeColor: Colors.blue,
                  value: controller.isForward.value,
                  onChanged: (value) => controller.isForward.value = value,
                ),
              ),
            ],
          ),
          SizedBox(height: 10),
          Row(
            children: [
              SizedBox(
                width: 80,
                child: Text('是否联网', style: TextStyle(fontSize: 18)),
              ),
              Obx(
                () => Switch(
                  activeColor: Colors.blue,
                  value: controller.isNetwork.value,
                  onChanged: (value) => controller.isNetwork.value = value,
                ),
              ),
            ],
          ),
          SizedBox(height: 10),
          Row(
            children: [
              SizedBox(
                width: 80,
                child: Text('转发次数', style: TextStyle(fontSize: 18)),
              ),
              SelectBox(
                initialValue: controller.ttlCount.text,
                options: ttlCountOptions,
                onChanged: (value)=>{
                  controller.ttlCount.text = value
                },
                label: '',
              ),
            ],
          ),
        ],
      ),
    );
  }
}
